/******************************************************************************
 *                       B O M B E R M A N  2 0 1 2
 * 
 * File Name: Bomb.java
 * 
 * COP 4331 Summer 2012 Project
 * 
 * Authors: Trevor Mancuso, Chris Chiarella, Zach Hoyler, Daniel Martin
 * 
 * 
 * Description: This class generates the bombs and destroys entities when 
 * exploding.
 * 
 ******************************************************************************/
package bomberman;

import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

public class Bomb extends Entity{
    
    int blastRadius;
    int time;
    Bomberman game;
    public Timer timer = new Timer();
    Bomber player;
    
    //Constructor
    Bomb(int x, int y, int blastRadius, int time, Bomberman game, Bomber player)
    {
        this.xPos = x;
        this.yPos = y;
        this.blastRadius = blastRadius;
        this.time = time;
        this.game = game;      
        this.player = player;
        
        setSprite(Bomberman.Sprites.BOMB.getSprite());;
        
        Sound.PLACEBOMB.play();
        
        //set countdown timer        
        timer.schedule(new BombCountdown(this), 0 , 1000);        
        
    }
    //When the timer hits 0, explode the bomb and destroy entities around it, 
    //creating explosion entities as well.
    public void explode()
    {
        int bombExploderArrayX[] = new int[blastRadius * 4];
        int bombExploderArrayY[] = new int[blastRadius * 4];
        int bombArrayPointer = 0;
        game.theGame.explosionArray[xPos][yPos] = new Explosion(xPos,yPos,game);
        
        Sound.EXPLOSION.play();
        
        //right
        for(int i=1; i <= blastRadius ; i ++)
        {
            boolean canExplode = true;
            boolean hitSomething = false;
            if(game.theGame.isBarrier(yPos, xPos+i))
            {
                if(game.theGame.barrierArray[yPos][xPos+i].isDestructable)
                {
                    if(game.theGame.barrierArray[yPos][xPos+i].hasPowerup)
                        game.theGame.powerupArray[xPos+i][yPos] = new Powerup(xPos+i,yPos,game);
                    game.theGame.barrierArray[yPos][xPos+i]=null;                    
                }
                else
                    canExplode = false;
                hitSomething = true;
            }
            if(xPos+i >= 0 && xPos+i <30)
            {
                for(int j=0; j < game.numPlayers; j++)
                {
                    if(game.bombers[j].getX() == xPos+i && game.bombers[j].getY() == yPos)
                        game.bombers[j].kill();
                }
                if(game.theGame.isBomb(xPos + i, yPos))
                {
                    bombExploderArrayX[bombArrayPointer] = xPos + i;
                    bombExploderArrayY[bombArrayPointer] = yPos;
                    bombArrayPointer++;
                    hitSomething = true;
                }
            }
            if(canExplode)
                game.theGame.explosionArray[xPos][yPos] = new Explosion(xPos + i,yPos,game);
            if(hitSomething)
                break;
            
        }
        //left
        for(int i=1; i <= blastRadius ; i ++)
        {
            boolean canExplode = true;
            boolean hitSomething = false;
            if(game.theGame.isBarrier(yPos, xPos-i))
            {
                if(game.theGame.barrierArray[yPos][xPos-i].isDestructable)
                {
                    if(game.theGame.barrierArray[yPos][xPos-i].hasPowerup)
                        game.theGame.powerupArray[xPos-i][yPos] = new Powerup(xPos-i,yPos,game);
                    
                    game.theGame.barrierArray[yPos][xPos-i]=null;
                }
                else
                    canExplode = false;
                hitSomething = true;
                
            }
            if(xPos-i >= 0 && xPos-i <30)
            {
                for(int j=0; j < game.numPlayers; j++)
                {
                    if(game.bombers[j].getX() == xPos -i && game.bombers[j].getY() == yPos)
                        game.bombers[j].kill();
                }
                if(game.theGame.isBomb(xPos - i, yPos))
                {
                    bombExploderArrayX[bombArrayPointer] = xPos - i;
                    bombExploderArrayY[bombArrayPointer] = yPos;
                    bombArrayPointer++;
                    hitSomething = true;
                }
            }
            if(canExplode)
                game.theGame.explosionArray[xPos][yPos] = new Explosion(xPos-i,yPos,game);
            if(hitSomething)
                break;
        }
        //up
        for(int i=1; i <= blastRadius ; i ++)
        {
            boolean canExplode = true;
            boolean hitSomething = false;
            if(game.theGame.isBarrier(yPos+i, xPos))
            {
                if(game.theGame.barrierArray[yPos+i][xPos].isDestructable)
                {
                    if(game.theGame.barrierArray[yPos+i][xPos].hasPowerup)
                        game.theGame.powerupArray[xPos][yPos+i] = new Powerup(xPos,yPos+i,game);
                    
                    game.theGame.barrierArray[yPos+i][xPos]=null;
                }
                else
                    canExplode = false;
                hitSomething = true;
            }
            if(yPos+i >= 0 && yPos+i <30)
            {
                for(int j=0; j < game.numPlayers; j++)
                {
                    if(game.bombers[j].getX() == xPos && game.bombers[j].getY() == yPos+i)
                        game.bombers[j].kill();
                }
                if(game.theGame.isBomb(xPos, yPos + i))
                {
                    bombExploderArrayX[bombArrayPointer] = xPos;
                    bombExploderArrayY[bombArrayPointer] = yPos + 1;
                    bombArrayPointer++;
                    hitSomething = true;
                }
            }
            if(canExplode)
                game.theGame.explosionArray[xPos][yPos] = new Explosion(xPos,yPos+i,game);
            if(hitSomething)
                break;
        }
        //down
        for(int i=1; i <= blastRadius ; i ++)
        {
            boolean canExplode = true;
            boolean hitSomething = false;
            if(game.theGame.isBarrier(yPos-i, xPos))
            {
                if(game.theGame.barrierArray[yPos-i][xPos].isDestructable)
                {
                    if(game.theGame.barrierArray[yPos-i][xPos].hasPowerup)
                        game.theGame.powerupArray[xPos][yPos-i] = new Powerup(xPos,yPos-i,game);
                    
                    game.theGame.barrierArray[yPos-i][xPos]=null;
                }
                else
                    canExplode = false;
                hitSomething = true;
            }
            if(yPos-i >= 0 && yPos-i <30)
            {
                for(int j=0; j < game.numPlayers; j++)
                {
                    if(game.bombers[j].getX() == xPos && game.bombers[j].getY() == yPos-i)
                        game.bombers[j].kill();
                }
            }
            if(game.theGame.isBomb(xPos, yPos - i))
            {
                bombExploderArrayX[bombArrayPointer] = xPos;
                bombExploderArrayY[bombArrayPointer] = yPos - i;
                bombArrayPointer++;
                hitSomething = true;
            }
            if(canExplode)
                game.theGame.explosionArray[xPos][yPos] = new Explosion(xPos,yPos-i,game);
            if(hitSomething)
                break;
        }
        //center
        
        for(int j=0; j < game.numPlayers; j++)
        {
            if(game.bombers[j].getX() == xPos && game.bombers[j].getY() == yPos)
                game.bombers[j].kill();
        }
          
        
        System.out.println("BOOM!");
        
        timer.cancel();
        player.numBombs--;
        game.theGame.bombArray[xPos][yPos] = null;
        player.lastBomb = null;
        for(int i = 0; i < bombArrayPointer; i++)
        {
            if(game.theGame.isBomb(bombExploderArrayX[i], bombExploderArrayY[i]))
                game.theGame.getBomb(bombExploderArrayX[i], bombExploderArrayY[i]).explode();
        }
    }
    //decrease the time left on the bomb
    public void countDown()
    {
        time--;
        System.out.println(time);
        if(time==0)
            this.explode();
    }
    //get the blast radius
    public int getRadius()
    {
        return blastRadius;
    } 
}

